﻿using HAF.Dispatcher.WorkItem;
using HAF.LogUtility.Assistantor;
using Iptv.MediaReporter.Entity;
using Iptv.MediaReporter.Helper;
using Iptv.MediaReporter.Model;
using RDS.Core;
using System;
using System.Collections.Generic;
using System.IO;

namespace Iptv.MediaReporter.SubItems
{
    /// <summary>
    /// 
    /// </summary>
    public class AuditReportItem : BaseWorkerItem, IDisposable
    {
        private LoadConfig _runConfig;
        /// <summary>
        /// 
        /// </summary>
        public void Dispose()
        {
            WriteDebugMessage("内容审核信息上报任务结束");
        }

        private void RefreshConfig()
        {
            //获取本地配置文件 每次执行的时候读取，实现热配置
            string loadConfigPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, RuntimeAdapter.PluginDir, "ReportConfig.json");
            ShowDebugMessage("获取本地配置文件{0}", loadConfigPath);
            _runConfig = JsonAssistant.FromFile<LoadConfig>(loadConfigPath);
        }

        /// <summary>
        /// 
        /// </summary>
        public override void ExecItem()
        {
            base.ExecItem();
            RefreshConfig();

            if (WorkItems != null && WorkItems is MediaEntity)
            {
                MediaEntity program = WorkItems as MediaEntity;
                PushTask(program);
            }
            else
            {
                this.Statu = HAF.Dispatcher.Enum.EnumRunItemStatu.End;
            }
        }

        private void PushTask(MediaEntity program)
        {
            try
            {
                var recordData = new ReportAuditData
                {
                    action = "10",
                    dataList = new List<AuditData>()
                    {
                        new AuditData()
                        {
                            guid = program.TaskID
                        }
                    }
                };

                string postData = JsonAssistant.Serialize(recordData);
                ShowDebugMessage("审核上报请求数据为 {0}", postData);
                string responseStr = WebHelper.GetHttpResponse(_runConfig.ReportApiUrl, "Post", "application/json", postData);
                ShowDebugMessage("审核上报响应数据为 {0}", responseStr);
                //需要取返回的GUID值
                if (!string.IsNullOrEmpty(responseStr))
                {
                    var recordResult = JsonAssistant.Deserialize<ReportResultData>(responseStr);
                    if (recordResult != null)
                    {
                        //WorkOutMessage outTaskMessage = new WorkOutMessage();
                        //outTaskMessage.ID = program.MediaID;
                        //if (recordResult.data != null && recordResult.data.Count > 0)
                        //{
                        //    string mediaGuid = recordResult.data[0].guid;
                        //    outTaskMessage.Code = int.Parse(recordResult.code);
                        //    outTaskMessage.Token = mediaGuid;
                        //    ShowDebugMessage(mediaGuid);
                        //}
                        //CallBack(program.MediaID, outTaskMessage);
                        EndProcess(program.JobID, recordResult.code == "0");
                    }

                    //打印失败信息
                    ShowDebugMessage(recordResult.message);
                }
            }
            catch (Exception ex)
            {
                EndProcess(program.JobID, false);
                ShowErrMessage(ex.ToString());
            }
        }
    }
}
